package com.jacky.dropdownmenu.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.jacky.dropdownmenu.R;
import com.jacky.dropdownmenu.adapter.CommonAdapter;
import com.jacky.dropdownmenu.adapter.ViewHolder;
import com.jacky.dropdownmenu.model.DropdownChild;
import com.jacky.dropdownmenu.model.DropdownGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jacky on 2017/2/23.
 * 下拉式菜单
 */

public class DropdownDoubleMenu extends RelativeLayout {


    private View mView;

    private Context mContext;

    private List<DropdownGroup> mGroups = new ArrayList<>();
    private List<DropdownChild> mChild = new ArrayList<>();
    private DropdownButton vBtn_dropdpwn;

    private ListView vListGroup, vListChild;
    private View vMask;
    private LinearLayout vLl;

    private boolean isOpen;

    private CommonAdapter<DropdownGroup> mAdapterGroup;
    private CommonAdapter<DropdownChild> mAdapterChild;

    //动画
    private Animation mDropdown_in, mDropdown_out, mDropdown_mask_out;

    private int mCurrentGroupPosition = 0;


    public interface onDropdownSelected {

        void onSelected(DropdownGroup group, int groupPosition,DropdownChild child, int childPosition);

        void onOpen();
    }

    private onDropdownSelected l;

    public void setDropdownSelected(onDropdownSelected l) {
        this.l = l;
    }


    public DropdownDoubleMenu(Context context) {
        super(context);
        mContext = context;
        setup();
    }

    public DropdownDoubleMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setup();
    }

    private void setup() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_dropdown_menu, null);
        vBtn_dropdpwn = (DropdownButton) mView.findViewById(R.id.btn_open);
        addView(mView);
        //初始动画
        mDropdown_in = AnimationUtils.loadAnimation(getContext(), R.anim.dropdown_in);
        mDropdown_out = AnimationUtils.loadAnimation(getContext(), R.anim.dropdown_out);
        mDropdown_mask_out = AnimationUtils.loadAnimation(getContext(), R.anim.dropdown_mask_out);

        vBtn_dropdpwn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGroups.size() != 0) {
                    if (isOpen) {
                        getClose();
                    } else {
                        getOpen();
                    }
                }
            }
        });

    }

    /**
     * 填充数据
     *
     * @param datas 数据源
     * @param ll    2个listview父布局
     * @param mask  模拟的背景蒙版
     */
    public void setData(List<DropdownGroup> datas, LinearLayout ll, View mask) {
        this.mGroups.clear();
        this.mGroups.addAll(datas);
        if (mGroups.size() != 0) {
           this. mChild.addAll(datas.get(0).getChild());
            this.  mGroups.get(0).setSelected(true);
        }
        this.vListGroup = (ListView) ll.getChildAt(0);
        this.vListChild = (ListView) ll.getChildAt(1);
        this.vLl = ll;
        this.vMask = mask;
        vMask.setVisibility(GONE);
        initAdapter();
        vMask.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getClose();
            }
        });
    }

    private void initAdapter() {
        //父级别
        mAdapterGroup = new CommonAdapter<DropdownGroup>(mContext, mGroups, R.layout.item_dropdown_double_group_menu) {
            @Override
            public void convert(ViewHolder holder, DropdownGroup item, int position) {
                if (item.isSelected()) {
                    holder.getView(R.id.bg_linear).setBackgroundColor(Color.parseColor("#25D5C4"));

                } else {
                    holder.getView(R.id.bg_linear).setBackgroundColor(Color.parseColor("#ffffff"));
                }

                holder.setText(R.id.title, item.getTitle());
            }
        };
        vListGroup.setAdapter(mAdapterGroup);
        vListGroup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCurrentGroupPosition =position;
                getResetGroupSelected();
                DropdownGroup group = mGroups.get(position);
                mChild.clear();
                mChild.addAll(group.getChild());
                group.setSelected(true);
                mAdapterChild.notifyDataSetChanged();
                mAdapterGroup.notifyDataSetChanged();
            }
        });
        //子级别
        mAdapterChild = new CommonAdapter<DropdownChild>(mContext, mChild, R.layout.item_dropdown_double_child_menu) {
            @Override
            public void convert(ViewHolder holder, DropdownChild item, int position) {
                if (item.isSelected()) {
                    holder.getView(R.id.bg_linear).setBackgroundColor(Color.parseColor("#25D5C4"));

                } else {
                    holder.getView(R.id.bg_linear).setBackgroundColor(Color.parseColor("#ffffff"));
                }

                holder.setText(R.id.title, item.getTitle());
            }
        };
        vListChild.setAdapter(mAdapterChild);
        vListChild.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                getResetSelected();
                mAdapterChild.getItem(position).setSelected(true);
                mAdapterChild.notifyDataSetChanged();
                vBtn_dropdpwn.setText(mAdapterChild.getItem(position).getTitle());
                getClose();
                if (l != null) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            l.onSelected(mGroups.get(position),mCurrentGroupPosition,mAdapterChild.getItem(position), position);
                        }
                    }, 100);
                }
            }
        });
    }

    /**
     * 重置选中状态
     */
    private void getResetGroupSelected() {
        if (mGroups.size() != 0) {
            for (DropdownGroup group :
                    mGroups) {
                group.setSelected(false);
            }
        }
    }


    /**
     * 重置选中状态
     */
    private void getResetSelected() {
        if (mGroups.size() != 0) {
            for (DropdownGroup group :
                    mGroups) {
                group.setSelected(false);
                for (DropdownChild child :
                        group.getChild()) {
                    child.setSelected(false);

                }
            }
        }
    }

    /**
     * 打开下菜菜单
     */
    public void getClose() {
        isOpen = false;
        vBtn_dropdpwn.setChecked(false);

        vLl.startAnimation(mDropdown_out);
        vLl.setVisibility(GONE);
        vMask.startAnimation(mDropdown_mask_out);
        vMask.setVisibility(GONE);
    }

    /**
     * 关闭菜单 不走动画
     * 一般有多个这个菜单的时候才用
     * 回调里面处理
     */
    public void getClear() {
        if (isOpen) {
            isOpen = false;
            vBtn_dropdpwn.setChecked(false);

            vLl.setVisibility(GONE);
            vMask.setVisibility(GONE);
        }
    }

    /**
     * 关闭下拉菜单
     */
    public void getOpen() {
        if (l != null) {
            l.onOpen();
        }
        isOpen = true;
        vBtn_dropdpwn.setChecked(true);
        vLl.setVisibility(VISIBLE);
        vLl.startAnimation(mDropdown_in);
        vMask.setVisibility(VISIBLE);
    }
}
